<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="确认状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="确认状态" clearable>
          <el-option
            v-for="dict in dict.type.sto_confirm"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stock:increase:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stock:increase:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stoIncList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="increaseId"/>
      <el-table-column label="确认状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sto_confirm" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="最后更新时间" align="center" prop="updateTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['stock:increase:edit']"
            v-show="scope.row.status==0"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stock:increase:remove']"
            v-show="scope.row.status==0"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleConfirm(scope.row)"
            v-hasPermi="['stock:increase:approve']"
            v-show="scope.row.status==0"
          >确认
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleQueryBody(scope.row)"
            v-if="scope.row.status!=0"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="序号" prop="increaseId">
              <el-input v-model="form.increaseId" placeholder="自动生成" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">子表信息</el-divider>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAddLine"
          >增行
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleDeleteLine"
          >删行
          </el-button>
        </el-col>
      </el-row>
      <el-table :data="form.bodyList" :row-class-name="rowBodyIndex"  @selection-change="handleBodySelection" ref="stoIncBody">
        <el-table-column type="selection" align="center" width="50"/>
        <el-table-column label="序号" align="center" prop="index" width="100"/>
        <el-table-column label="图书序号" align="center" prop="bookId">
          <template slot-scope="scope">
            <el-input v-model="scope.row.bookId" placeholder="请输入图书序号"/>
          </template>
        </el-table-column>
        <el-table-column label="补充数量" align="center" prop="number" width="220">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.number" :precision="0" :step="1" :min="0"/>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog title="详情" :visible.sync="bodyOpen" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="序号" prop="increaseId">
              <el-input v-model="form.increaseId" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">子表信息</el-divider>
      <el-table :data="form.bodyList" :row-class-name="rowBodyIndex" ref="stoIncBody">
        <el-table-column label="序号" align="center" prop="index" width="100"/>
        <el-table-column label="图书序号" align="center" prop="bookId"/>
        <el-table-column label="补充数量" align="center" prop="number" width="220"/>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { addStoInc, delStoInc, getStoInc, listStoInc, updateStoInc, confirmStoInc} from '@/api/stock/increase'

export default {
  name: 'Increase',
  dicts: ['sto_confirm'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库表格数据
      stoIncList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查看弹出层
      bodyOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        number: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ],
        bookId: [
          { required: true, message: '图书序号不能为空', trigger: 'blur' }
        ]
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询入库列表 */
    getList() {
      this.loading = true
      listStoInc(this.queryParams).then(response => {
        this.stoIncList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        increaseId: undefined,
        bodyList: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.increaseId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加入库'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const increaseId = row.increaseId || this.ids
      getStoInc(increaseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改数据'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.increaseId != undefined) {
            updateStoInc(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addStoInc(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const increaseId = row.increaseId || this.ids
      this.$modal.confirm('是否确认删除编号为"' + increaseId + '"的数据项？').then(function() {
        return delStoInc(increaseId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 子信息序号 */
    rowBodyIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 复选框选中方法 */
    handleBodySelection(selection) {
      this.checkedBody = selection.map(item => item.index)
    },
    /** 确认按钮 */
    handleConfirm(row) {
      this.$modal.confirm('是否确认编号为"' + row.increaseId + '"的数据项？').then(function() {
        return confirmStoInc(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
      })
    },
    /** 查看按钮 */
    handleQueryBody(row) {
      getStoInc(row.increaseId).then(response => {
        this.form = response.data
        this.bodyOpen = true
      })
    },
    /** 增行按钮操作 */
    handleAddLine() {
      let obj = {}
      obj.bookId = null
      obj.number = '0.00'
      this.form.bodyList.push(obj)
    },
    /** 删行按钮操作 */
    handleDeleteLine() {
      if (this.checkedBody.length === 0) {
        this.$modal.msgError('请先选择需要删除的子数据')
      } else {
        const bodyList = this.form.bodyList
        const checkedBodyList = this.checkedBody
        this.form.bodyList = bodyList.filter(function(item) {
          return checkedBodyList.indexOf(item.index) === -1
        })
      }
    },
  }
}
</script>
